package sfeir.gwt.ergosum.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;

import sfeir.gwt.ergosum.client.ClientApi.NotLoggedInException;
import sfeir.gwt.ergosum.client.model.Address;
import sfeir.gwt.ergosum.client.model.Email;
import sfeir.gwt.ergosum.client.model.GeoLocalisation;
import sfeir.gwt.ergosum.client.model.ImageURL;
import sfeir.gwt.ergosum.client.model.NetworkItem;
import sfeir.gwt.ergosum.client.model.Person;
import sfeir.gwt.ergosum.client.model.Tel;
import sfeir.gwt.ergosum.server.guice.GuiceServletContextListener;
import sfeir.gwt.ergosum.server.service.ProfileService;
import sfeir.gwt.ergosum.server.service.ProfileServiceImpl;

@SuppressWarnings("serial")
@Singleton
public class VcardProfile extends HttpServlet {

    @Inject
    protected ProfileService profileService;

    @Override
    public void init() throws ServletException {
        Injector injector = (Injector) getServletContext().getAttribute(GuiceServletContextListener.INJECTOR_NAME);
        injector.injectMembers(this);
        // profileService = new ProfileServiceImpl();
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String username = req.getPathInfo();
       if (username != null) {
           username = username.substring(1);
       } else {
           username = "";
       }
       Person p = profileService.getProfile(username);
       resp.setHeader("Content-type", "text/x-vcard");
       PrintWriter str = resp.getWriter();
       str.append("BEGIN:VCARD\r\n");
       str.append("VERSION:3.0\r\n");
       str.append("N:");
       str.append(p.getName());
       str.append(';');
       str.append(p.getFirstname());
       str.append(";;;\r\n");
       str.append("FN:");
       str.append(p.getFullName());
       str.append("\r\n");
       if (p.getNickname() != null) {
           str.append("NICKNAME:");
           str.append(p.getNickname());
           str.append("\r\n");
       }
       if (p.getAboutMe() != null) {
           str.append("NOTE:");
           str.append(p.getAboutMe());
           str.append("\r\n");
       }
       if (p.getCompany() != null) {
           str.append("ORG:");
           str.append(p.getCompany());
           str.append("\r\n");
       }
       if (p.getTitle() != null) {
           str.append("TITLE:");
           str.append(p.getTitle());
           str.append("\r\n");
       }
       if (p.getBirthDay() != null) {
           str.append("BDAY:");
           str.append(p.getBirthDay());
           str.append("\r\n");
       }
       if (p.getPhoto() != null) {
           str.append("URL:");
           str.append(p.getPhoto().getImageUrl());
           str.append("\r\n");
       }
       List<Tel> tels = p.getTels();
       for (Tel tel : tels) {
           str.append("TEL");
           switch (tel.getType()) {
                case Tel.HOME_LANDLINE:
                    str.append(";TYPE=HOME:");
                    break;
                case Tel.MOBILE_LINE:
                    str.append(";TYPE=CELL:");
                    break;
                case Tel.PROFESSIONNAL_LINE:
                    str.append(";TYPE=WORK:");
                    break;
                default:
                    str.append(':');
                    break;
           }
           str.append(tel.getPhoneNumber());
           str.append("\r\n");
       }
       List<Email> emails = p.getEmails();
       for (Email email : emails) {
           str.append("EMAIL");
           switch (email.getTypeEmail()) {
                case Tel.HOME_LANDLINE:
                    str.append(";TYPE=HOME:");
                    break;
                case Tel.MOBILE_LINE:
                    str.append(";TYPE=CELL:");
                    break;
                case Tel.PROFESSIONNAL_LINE:
                    str.append(";TYPE=WORK:");
                    break;
                default:
                    str.append(':');
                    break;
           }
           str.append(email.getEmail());
           str.append("\r\n");
       }
       List<Address> addresses = p.getAddresses();
       for (Address addresse : addresses) {
           str.append("ADR");
           switch (addresse.getType()) {
                case Address.PERSONAL_ADDRESS:
                    str.append(";TYPE=HOME:");
                    break;
                case Address.PROFESSIONNAL_ADDRESS:
                    str.append(";TYPE=WORK:");
                    break;
                default:
                    str.append(':');
                    break;
           }
           str.append(addresse.getFullValue());
           str.append("\r\n");
       }
       List<NetworkItem> networks = p.getNetworkItems();
       for (NetworkItem network : networks) {
           str.append("URL:");
           str.append(network.getNetworkItemLink());
           str.append("\r\n");
       }
       if (p.getGeo() != null) {
           str.append("GEO:");
           str.append(Double.toString(p.getGeo().getLatitude()));
           str.append(';');
           str.append(Double.toString(p.getGeo().getLongitude()));
           str.append("\r\n");
       }
       str.append("X-GENERATOR:ErgoSum\r\n");
       str.append("END:VCARD\r\n");

//TEL;TYPE=HOME:+33149600898
//TEL;TYPE=CELL:+33675950150
//GEO:48.868215;2.227306
//ADR;TYPE=HOME;CHARSET=UTF-8:;;7 rue Anatole France;Le Kremlin-Bicêtre;Val-d
// e-Marne;94270;France
//N;CHARSET=UTF-8:Cappelle;Florent;;;
//FN;CHARSET=UTF-8:Florent Cappelle
//NICKNAME;CHARSET=UTF-8:Bigneotux
//NOTE;CHARSET=UTF-8:Get ErgoSum !
//X-GENERATOR:telProxy vCard 1.0
//END:VCARD");
    }
}
